var block = document.querySelector('.block')
var bg = document.querySelector('.bg')
var sild = document.querySelector('.sild')
var p = document.querySelector('.sild p')
var span = document.querySelector('.sild span')
var b = document.querySelector('.sild b')

var obj = {
  phone: false,
  pwd1: false,
  pwd2: false,
  move: false,
  code: false,
  check: false
}
//按下
block.onmousedown = function (ev) {
  var success = false
  ev = ev || event
  var oldX = ev.clientX
  //移动
  document.onmousemove = function (ev) {
    var x = ev.clientX - oldX//移动的距离
    var w = sild.offsetWidth - block.offsetWidth//整体减去块的宽度
    block.style.left = x + 'px'
    bg.style.width = x + 'px'
    if (x < 0) {
      block.style.left = 0 + 'px'
      bg.style.width = 0 + 'px'
    } else if (x >= w) {
      block.style.left = w + 'px'
      bg.style.width = w + 'px'
      b.style.display = 'none'
      span.style.display = 'inline-block'
      p.innerHTML = '验证通过'
      p.style.color = '#fff'
      p.style.left = w / 2 + 'px'
      success = true;
      document.onmousemove = null
      block.onmousedown = null
    }
  }
  //抬起
  document.onmouseup = function () {
    if (success) {
      obj.move = true
      return
    } else {
      obj.move = false
      block.style.left = 0 + 'px'
      bg.style.width = 0 + 'px'
    }
    document.onmousemove = null
    document.onmouseup = null
  }
}


let sj = document.querySelector('.sj')
let pw1 = document.querySelector('.pw1')
let pw2 = document.querySelector('.pw2')
let yz = document.querySelector('#yz')
let yzm = document.querySelector('.yzm')
let read = document.querySelector('.read')
let zhuce = document.querySelector('.zhuce')
sj.addEventListener('blur', function () {
  let reg = /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/
  let str = sj.value
  if (!reg.test(str)) {
    sj.nextElementSibling.style.display = 'block'
    obj.phone = false
  }
  if (reg.test(str)) {
    sj.nextElementSibling.style.display = 'none'
    obj.phone = true
  }
})

pw1.addEventListener('blur', function () {
  let reg = /^[a-zA-Z]\w{5,19}$/
  let str = pw1.value
  if (!reg.test(str)) {
    pw1.nextElementSibling.style.display = 'block'
    obj.pwd1 = false
  }
  if (reg.test(str)) {
    pw1.nextElementSibling.style.display = 'none'
    obj.pwd1 = true
  }
})

pw2.addEventListener('blur', function () {
  if (pw2.value !== pw1.value) {
    pw2.nextElementSibling.style.display = 'block'
    obj.pwd2 = false
  }
  if (pw2.value == pw1.value) {
    pw2.nextElementSibling.style.display = 'none'
    obj.pwd2 = true
  }
})
var dflag = false
let n = null
if (!dflag) {
  yz.addEventListener('click', function () {
    dflag = true
    n = parseInt(Math.random() * (9999 - 1000 + 1) + 1000);
    alert('验证码是:' + n)
    var i = 30
    function countd() {
      if (i >= 0) {
        yz.innerHTML = i + '秒后重新发送'
        yz.className = 'yz'
        i--
      } else {
        yz.innerHTML = '获取短信验证码'
        yz.className = ''
        dflag = false
        clearInterval(timer)
      }
    }
    countd()
    var timer = setInterval(countd, 1000)
  })
}

yzm.addEventListener('blur', function () {
  if (yzm.value != n) {
    yz.nextElementSibling.style.display = 'block'
    obj.code = false
  }
  if (yzm.value == n) {
    yz.nextElementSibling.style.display = 'none'
    obj.code = true
  }
})
read.addEventListener('change', function () {
  if (read.checked) {
    obj.check = true
  } else {
    obj.check = false
  }
})


zhuce.addEventListener('click', function () {
  let flag = true
  for (k in obj) {
    if (!obj[k]) {
      flag = false
    }
  }
  if (flag) {
    pAjax({
      url: '../server/isregist.php',
      type: 'post',
      data: {
        username: sj.value
      }
    }).then(res => {
      if(res){
        alert(res)
      } else {
        pAjax({
          url: '../server/regist.php',
          type: 'post',
          data: {
            username: sj.value,
            password: pw1.value
          }
        }).then(() => {
      alert('注册成功')
    })
      }
    })

  }
})